package com.cindata

import grails.transaction.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import com.cindata.dictionary.*

@Transactional
@CompileStatic
@TypeChecked
class AbnormalMortgageBatchService
{

    static scope = "singleton"

    AbnormalMortgageBatchFlowStageService abnormalMortgageBatchFlowStageService
    AbnormalMortgageBatchFlowStageConditionService abnormalMortgageBatchFlowStageConditionService
    AbnormalMortgageBatchFlowStageRoleService abnormalMortgageBatchFlowStageRoleService

    // 初始化不良资产包登记流程
    def initAbnormalMortgageBatchFlowStage(AbnormalMortgageBatch abnormalMortgageBatch, Workflow workflow = null)
    {
        // if (!workflow)
        // {
        //     def account = abnormalMortgageBatch?.createBy?.account
        //     workflow = AccountFlow.find("from AccountFlow where account.id = ? and type.id = ?", [account?.id, abnormalMortgageBatch.type?.id])?.workflow
        // }

        if (workflow)
        {
            abnormalMortgageBatchFlowStageService.initFlowStage(abnormalMortgageBatch, workflow)
            abnormalMortgageBatchFlowStageConditionService.initFlowStageCondition(abnormalMortgageBatch, workflow)
            abnormalMortgageBatchFlowStageRoleService.initFlowStageRole(abnormalMortgageBatch, workflow)

            def flowFirstStage = WorkflowStage.find("from WorkflowStage where workflow.id = ? order by executionSequence asc", [workflow?.id])
            def abnormalMortgageBatchFlowStage = AbnormalMortgageBatchFlowStage.find("from AbnormalMortgageBatchFlowStage where batch.id = ? and stage.id = ? order by executionSequence asc", [abnormalMortgageBatch?.id, flowFirstStage?.stage.id])
            if (abnormalMortgageBatchFlowStage)
            {
                abnormalMortgageBatchFlowStage.startTime = new Date()
                abnormalMortgageBatchFlowStage.save flush: true

                abnormalMortgageBatch.stage = AbnormalMortgageBatchStage.find("from AbnormalMortgageBatchStage where id = ?", [flowFirstStage?.stage?.id])
                abnormalMortgageBatch.status = "Pending"
                abnormalMortgageBatch.save flush: true
            }
        }
        else
        {
            println "未找到工作流，不良资产包 ${abnormalMortgageBatch?.name} 初始化失败"
        }
    }

    // 推进
    def approve(AbnormalMortgageBatch abnormalMortgageBatch)
    {
        def map = [:]

        def currentFlowStage = AbnormalMortgageBatchFlowStage.find("from AbnormalMortgageBatchFlowStage where batch.id = ? and stage.id = ? order by executionSequence asc", [abnormalMortgageBatch?.id, abnormalMortgageBatch?.stage.id])

        // 验证
        def verifyCondition = abnormalMortgageBatchFlowStageConditionService.evaluate(abnormalMortgageBatch)
        if (!verifyCondition['flag'])
        {
            return verifyCondition
        }

        def nextFlowStage = AbnormalMortgageBatchFlowStage.find("from AbnormalMortgageBatchFlowStage where batch.id = ? and executionSequence > ? order by executionSequence asc", [abnormalMortgageBatch?.id, currentFlowStage?.executionSequence])
        if (!nextFlowStage)
        {
            map['flag'] = false
            map['message'] = "找不到工作流"
            return map
        }

        // 如果 nextFlowStage 为工作流的最后一个阶段，则将 nextFlowStage endTime 赋值成当前时间,将不良资产包状态赋值成 Completed
        def lastFlowStage = AbnormalMortgageBatchFlowStage.find("from AbnormalMortgageBatchFlowStage where batch.id = ? and executionSequence > ? order by executionSequence asc", [abnormalMortgageBatch?.id, nextFlowStage?.executionSequence])

        currentFlowStage.endTime = new Date()
        currentFlowStage.processed = true
        currentFlowStage.save flush: true

        nextFlowStage.startTime = new Date()
        if (!lastFlowStage)
        {
            nextFlowStage.endTime = new Date()
            abnormalMortgageBatch.status = "Completed"
        }
        nextFlowStage.save flush: true

        abnormalMortgageBatch.stage = (AbnormalMortgageBatchStage) nextFlowStage?.stage
        abnormalMortgageBatch.save flush: true

        map['flag'] = true
        map['message'] = "成功"
        return map
    }

    // 拒绝
    Boolean reject(AbnormalMortgageBatch abnormalMortgageBatch)
    {
        def flag = false
        if (abnormalMortgageBatch?.status == "Pending")
        {
            def currentFlowStage = AbnormalMortgageBatchFlowStage.find("from AbnormalMortgageBatchFlowStage where batch.id = ? and stage.id = ? order by executionSequence asc", [abnormalMortgageBatch?.id, abnormalMortgageBatch?.stage?.id])
            if (currentFlowStage?.canReject)
            {
                def previousFlowStage = AbnormalMortgageBatchFlowStage.find("from AbnormalMortgageBatchFlowStage where batch.id = ? and executionSequence < ? and processed = true order by executionSequence desc", [abnormalMortgageBatch?.id, currentFlowStage?.executionSequence])
                if (previousFlowStage)
                {
                    abnormalMortgageBatch.stage = (AbnormalMortgageBatchStage) previousFlowStage.stage
                    if (abnormalMortgageBatch.validate())
                    {
                        abnormalMortgageBatch.save flush: true
                        flag = true
                    }
                    else
                    {
                        abnormalMortgageBatch.errors.each {
                            log.info "${it}"
                        }
                    }
                }
            }
        }
        flag
    }
}
